home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / PSTOPPM.PS < prev    next >
Text File  |  1992-01-17  |  7KB  |  208 lines

  1. %! PS-Adobe-2.0
  2. %% Program for reading a .ps file and writing out a PPM file.
  3. %% For Ghostscript 2.4.
  4. %%
  5. %% Modified by L. Peter Deutsch 1/17/92:
  6. %%   the palette for makeimagedevice is now a string, not an array.
  7. %% Modified by L. Peter Deutsch 9/24/91:
  8. %%   allow starting page number to be specified.
  9. %% Modified by L. Peter Deutsch 7/7/91 to keep track of page count
  10. %%   in a way that gets around save and restore.
  11. %% Modified by L. Peter Deutsch 11/07/90
  12. %%   to use filename.ppm for the first page, renamed to filename.1ppm
  13. %%   with subsequent pages .2ppm, etc. if more than one page.
  14. %% Modified by Henry Minsky 11/03/90
  15. %%   for each showpage, it writes out a ppm file with name filename.ppm.N
  16. %%   where N increments each showpage, starting at 1
  17. %% Modified by L. Peter Deutsch -- Aladdin Enterprises -- 08/25/90 --
  18. %%   converted from a one-shot program to a utility package,
  19. %%   designed to be used from an interactive terminal.
  20. %% Modified by L. Peter Deutsch -- Aladdin Enterprises -- 08/02/90
  21. %% Modified on 08/02/90 for using the CORRECT color map.
  22. %% Modified 06/26/90 for a color file
  23. %% Original version by Phillip Conrad - Perfect Byte, Inc.
  24. %%
  25. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  26.  
  27. %%%%%% Define the directory for holding the PPM information
  28.  
  29. /ppmdict 30 dict def
  30. ppmdict begin
  31.  
  32.   /Horz_dpi 72 def        % defaults to screen density
  33.   /Vert_dpi 72 def
  34.  
  35.   /OutFilePrefix () def        % default to null (current directory)
  36.  
  37.   /FirstPageNumber 1 def    % any non-negative integer is OK
  38.  
  39.   /Palette1 <ff 00> def
  40.   /Palette8
  41. %========== Here we define all 256 color entries. ======
  42. %========== Colors were taken from the X Windows default colors. ======
  43. <
  44. 000000 ffffff a8a8a8 ebebeb 5c5c5c 373737 5f929e 85ccdd
  45. 345057 1f3034 729efe a0ddff 3f578c 263454 6186d8 2f2f64
  46. ffff00 b0e2ff ff0000 808080 efdf84 55fe55 fe0000 66fe57
  47. fe987a feca71 fefefe fe8d7c fea977 fec472 feb875 fe937b
  48. fe957a feb575 98fe5d fe8b7d fea677 feb276 feaf76 febe74
  49. fe837e fefc6a fe0c9a fe0350 fe08b5 6715fe fefb3f fe544b
  50. 00c000 20c000 40c000 60c000 80c000 a0c000 c0c000 e0c000
  51. 00e000 20e000 40e000 60e000 80e000 a0e000 c0e000 e0e000
  52. 000040 200040 400040 600040 800040 a00040 c00040 e00040
  53. 002040 202040 402040 602040 802040 a02040 c02040 e02040
  54. 004040 204040 404040 604040 804040 a04040 c04040 e04040
  55. 006040 206040 406040 606040 806040 a06040 c06040 e06040
  56. 008040 208040 408040 608040 808040 a08040 c08040 e08040
  57. 00a040 20a040 40a040 60a040 80a040 a0a040 c0a040 e0a040
  58. 00c040 20c040 40c040 60c040 80c040 a0c040 c0c040 e0c040
  59. 00e040 20e040 40e040 60e040 80e040 a0e040 c0e040 e0e040
  60. 000080 200080 400080 600080 800080 a00080 c00080 e00080
  61. 002080 202080 402080 602080 802080 a02080 c02080 e02080
  62. 004080 204080 404080 604080 804080 a04080 c04080 e04080
  63. 006080 206080 406080 606080 806080 a06080 c06080 e06080
  64. 008080 208080 408080 608080 808080 a08080 c08080 e08080
  65. 00a080 20a080 40a080 60a080 80a080 a0a080 c0a080 e0a080
  66. 00c080 20c080 40c080 60c080 80c080 a0c080 c0c080 e0c080
  67. 00e080 20e080 40e080 60e080 80e080 a0e080 c0e080 e0e080
  68. 0000c0 2000c0 4000c0 6000c0 8000c0 a000c0 c000c0 e000c0
  69. 0020c0 2020c0 4020c0 6020c0 8020c0 a020c0 c020c0 e020c0
  70. 0040c0 2040c0 4040c0 6040c0 8040c0 a040c0 c040c0 e040c0
  71. 0060c0 2060c0 4060c0 6060c0 8060c0 a060c0 c060c0 e060c0
  72. 0080c0 2080c0 4080c0 6080c0 8080c0 a080c0 c080c0 e080c0
  73. 00a0c0 20a0c0 40a0c0 60a0c0 80a0c0 a0a0c0 c0a0c0 e0a0c0
  74. 00c0c0 20c0c0 40c0c0 60c0c0 80c0c0 a0c0c0 c0c0c0 e0c0c0
  75. 00e0c0 20e0c0 40e0c0 60e0c0 80e0c0 a0e0c0 c0e0c0 e0e0c0
  76. > def
  77.  
  78.   % Define a procedure for computing the output file name for a given page.
  79.   /pagefilename        % <int|null> pagefilename -> <string
  80.    { OutFilePrefix FileName concatstrings (.) concatstrings
  81.      exch dup null ne
  82.       { 1 sub FirstPageNumber add (xxxxxx) cvs concatstrings }
  83.       { pop }
  84.      ifelse (ppm) concatstrings
  85.    } def
  86.  
  87.   % Redefine copypage and showpage appropriately.
  88.   /copypage
  89.    { ppmdict begin
  90.  
  91.        /PageCount PageCountString cvi 100000 sub def
  92.        /PageCount PageCount 1 add def
  93.  
  94.        PageCount 1 eq
  95.     { null pagefilename            % first page
  96.     }
  97.     { PageCount 2 eq
  98.        {                    % second page, rename first
  99.          null pagefilename 1 pagefilename
  100.          (Renaming ) print 1 index print ( to ) print
  101.            dup print (\n) print
  102.          flush
  103.          renamefile
  104.        } if
  105.       PageCount pagefilename
  106.     } ifelse
  107.        /FileNameOut exch def
  108.  
  109.        FileNameOut (w) file
  110.          /FileDescOut exch def
  111.  
  112.        (Writing ) print FileNameOut print (\n) print
  113.        flush
  114.        FileDescOut Device writeppmfile
  115.        FileDescOut closefile
  116.  
  117.        WrotePage 0 1 put    % /WrotePage true def
  118.        PageCount 100000 add PageCountString cvs pop
  119.  
  120.      end
  121.    } userdict begin def end
  122.   /showpage
  123.    { copypage erasepage initgraphics
  124.    } userdict begin def end
  125.  
  126.   /Convert            % the main procedure
  127.                   % <filename> <palette> Convert -
  128.    { /Palette exch def
  129.      /FileName exch def
  130.  
  131.      % Save and restore don't save and restore the contents of strings.
  132.      % Therefore, we use strings to hold the two variables whose values
  133.      % must persist across page boundaries (PageCount and WrotePage).
  134.  
  135.      /PageCountString 6 string def
  136.      100000 PageCountString cvs pop
  137.      /WrotePage 1 string def
  138.  
  139.      /ScaleX Horz_dpi 72 div def
  140.      /ScaleY Vert_dpi 72 div def
  141.  
  142.      /Width 85 Horz_dpi mul 5 add 10 div cvi def % add 5 to round up!
  143.      /Height 11 Vert_dpi mul def
  144.  
  145.      FileName (.ps) concatstrings
  146.        /FileNameIn exch def        % file name with extension
  147.  
  148.      [ScaleX 0.0 0.0 ScaleY neg 0.0 Height]
  149.      Width Height Palette makeimagedevice
  150.        /Device exch def
  151.      Device setdevice
  152.  
  153.      % For running the file, remove ppmdict from the dict stack
  154.      FileNameIn end run        % ppmdict
  155.      ppmdict begin
  156.      WrotePage 0 get 0 eq { showpage } if % make sure the page got written
  157.      end            % ppmdict
  158.  
  159.    } def
  160.  
  161. end                % ppmdict
  162.  
  163. %%%%%% Define the user-callable procedures
  164.  
  165. /ppmsetdensity
  166.  { ppmdict begin
  167.    /Vert_dpi exch def
  168.    /Horz_dpi exch def
  169.    end
  170.  } def
  171.  
  172. /ppmsetprefix
  173.  { ppmdict begin
  174.    /OutFilePrefix exch def
  175.    end
  176.  } def
  177.  
  178. /ppmsetfirstpagenumber
  179.  { cvi
  180.    ppmdict begin
  181.    /FirstPageNumber exch def
  182.  } def
  183.  
  184. /ppm1run
  185.  { ppmdict begin   Palette1 Convert
  186.  } def
  187.  
  188. /ppm8run
  189.  { ppmdict begin   Palette8 Convert
  190.  } def
  191.  
  192. /ppm24run
  193.  { ppmdict begin   null Convert
  194.  } def
  195.  
  196. %%%%%% Display instructions for the user.
  197.  
  198. (Usage: (file) ppmNrun\n) print
  199. (   converts file.ps to file.ppm (single page),\n) print
  200. (     or file.1ppm, file.2ppm, ... (multi page).\n) print
  201. (   N is # of bits per pixel (1, 8, or 24).\n) print
  202. (Examples: (golfer) ppm1run   ..or..   (escher) ppm8run\n) print
  203. (Optional commands you can give first:\n) print
  204. (   horiz_DPI vert_DPI ppmsetdensity\n) print
  205. (   (dirname/) ppmsetprefix\n) print
  206. (   page_num ppmsetfirstpagenumber\n) print
  207. flush
  208.